#include <head.h>

int main(int argc, char const *argv[])
{
    // 1 创建套接字
    int sok_fd = socket(AF_INET, SOCK_STREAM, 0);
    if (sok_fd == -1)
    {
        perror("socket");
        exit(1);
    }
    // 填充服务器结构体
    struct sockaddr_in sok_addr;
    sok_addr.sin_family = AF_INET;
    sok_addr.sin_port = htons(9090);
    sok_addr.sin_addr.s_addr = inet_addr("192.168.10.18");
    socklen_t len = sizeof(sok_addr);

    // 与服务器建立连接
    connect(sok_fd, (struct sockaddr *)&sok_addr, len);

    char buf[128];
    printf("开始聊天：");
    while (1)
    {

        memset(buf, 0, sizeof(buf));
        read(sok_fd, buf, sizeof(buf));
        printf("服务端输入:%s\n", buf);

        memset(buf, 0, sizeof(buf));
        printf("客户端输入:");
        fgets(buf, sizeof(buf), stdin);
        if (!strcmp(buf, "quit"))
        {
            write(sok_fd, buf, strlen(buf));
            break;
        }
        write(sok_fd, buf, strlen(buf));
    }
    return 0;
}
